<?php
/************************FoxPHP威客系统************************/
/*                                                            */
/*  FoxPHP.Com (C)2006-2016 FoxPHP Inc.                       */
/*  This is NOT a freeware, use is subject to license terms   */
/*  FoxPHP威客系统是商业软件,使用于商业用途请购买授权         */
/*  V3.0 2016.3                                               */
/*  foxphp@aliyun.com                                         */
/*  http://www.foxphp.com                                     */
/*                                                            */
/**************************************************************/

namespace Admin\Model;
use Think\Model;

class CategoryCityModel extends AdminModel {
	
	//数据过滤-----------------------------------------------------------------------------------------------
	function FormatData($manage=array()){
		$data['catid']=I('get.catid',0,'int');
		$data['upid']=I('post.upid',0,'int');
		$data['name']=I('post.name');
		$data['color']=I('post.color');
		$data['code']=I('post.code');
		$data['displayorder']=I('post.displayorder',0,'int');
		$data['SecurityForm']=I('post.SecurityForm');
		return $data;
	}

	function UpdateCategoryCityJson($level=1){
		
		set_time_limit(0);
		
		$where=' level=1 ';
		if($level==1){
			$where=' apiid>0 ';
		}
		
		$citylevel1array=M('CategoryCity')->token(false)->where($where)->field('catid,upid,name')->order('catid ASC')->select();
		
		$json='[';
		if($citylevel1array){
			$cityleveljson1='';
			foreach($citylevel1array as $citylevel1){
				$catid1=intval($citylevel1['catid']);
				
				if($level!=1){
					$citylevel2array=M('CategoryCity')->token(false)->where(" upid='$catid1' ")->field('catid,upid,name')->order('catid ASC')->select();
					
					$cityleveljson2='';
					if($citylevel2array){
						foreach($citylevel2array as $citylevel2){
							
							$catid2=intval($citylevel2['catid']);
							$citylevel3array=M('CategoryCity')->token(false)->where(" upid='$catid2' ")->field('catid,upid,name')->order('catid ASC')->select();
							
							$cityleveljson3='';
							if($citylevel3array){
								foreach($citylevel3array as $citylevel3){
									if($cityleveljson3){
										$cityleveljson3.=',';
									}
									
									$cityleveljson3.='{"s":"'.$citylevel3['name'].'","id":'.$citylevel3['catid'].'}';
								}
							}
							
							if($cityleveljson2){
								$cityleveljson2.=',';
							}
							
							$cityleveljson2.='{"n":"'.$citylevel2['name'].'","id":'.$citylevel2['catid'].',"a":['.$cityleveljson3.']}';
							
						}
					}
				}
				
				if($cityleveljson1){
					$cityleveljson1.=',';
				}
				
				if($level==1){
					$cityleveljson1.='{"p":"'.$citylevel1['name'].'","id":'.$citylevel1['catid'].'}';
				}else{
					$cityleveljson1.='{"p":"'.$citylevel1['name'].'","id":'.$citylevel1['catid'].',"c":['.$cityleveljson2.']}';
				}
				
			}

		}
		
		$json.=$cityleveljson1.']';
		
		if($level==1){
			WriteFile(FOXPHP_PATH.'/assets/js/foxphp_json_city.js',$json);
		}else{
			WriteFile(FOXPHP_PATH.'/assets/js/foxphp_json_city_all.js',$json);
		}
		
	}
	
}

?>